"""
Case Type   : 小型化
Case Name   : 环境变量设置-D路径，执行安装脚本不传-D参数（-D目录下不为空）
Create At   : 2024/10/24
Owner       : ningyali
Description :
    1.获取安装包并解压
    2.-D目录不为空
    3.环境变量设置-D路径
    4.执行安装脚本不传-D参数（-D目录下不为空）
    5.清理数据环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.失败
    5.清理完成
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_nodes = (Node('PrimaryRoot'),)
        self.user_nodes = (Node('PrimaryDbUser'),)
        self.user = None
        self.port = None
        self.assert_msg = f'config datanode failed, pleae check whether ' \
                          f'the database is instantiated properly'

    def test_upgrade(self):
        text = '----prestep1:获取可用用户和端口----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            *self.root_nodes)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_nodes[0].ssh_user = self.user
        self.user_nodes[0].ssh_password = macro.COMMON_PASSWD

        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, *self.root_nodes)

        text = '----step1:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_nodes[0])
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----step2:-D目录不为空;expect:修改成功----'
        self.log.info(text)
        cmd = f"mkdir -p {self.data_path}/test_dir;" \
              f"echo 123 > {self.data_path}/lite_placeholder.txt;" \
              f"ls {self.data_path};"
        self.log.info(cmd)
        res = self.user_nodes[0].sh(cmd).result()
        self.log.info(res)
        self.assertIn('test_dir', res, f'执行失败: {text}')
        self.assertIn('lite_placeholder.txt', res, f'执行失败: {text}')

        text = '----step3:环境变量设置-D路径;expect:成功----'
        self.log.info(text)
        cmd = f'echo "export GAUSSDATA={self.data_path}">>~/.bashrc;' \
              f'source ~/.bashrc;'
        self.log.info(cmd)
        res = self.user_nodes[0].sh(cmd).result()
        self.log.info(res)
        self.assertEqual(len(res.strip()), 0, f'执行失败: {text}')

        text = '----step4:执行安装脚本不传-D参数;expect:安装成功----'
        self.log.info(text)
        param = f'-R {self.app_path} -C "port={self.port}"'
        res = self.com_install.install_single(self.user_nodes[0], self.port,
                                              param)
        self.assertIn(self.assert_msg, res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes)
        self.com_uninstall.clear_env(*self.root_nodes)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
